//
//  UIImageView+PP.m
//  pickpic
//
//  Created by Jinxiao on 8/27/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "UIImageView+PP.h"

#import "PPImageTransaction.h"

@interface UIImageView (PP_)
@property (readwrite, nonatomic, strong) UIControl *control;
@end

@implementation UIImageView (PP)

- (void)setArtwork:(PPAbstractArtwork *)artwork {
    objc_setAssociatedObject(self, "artwork", artwork, OBJC_ASSOCIATION_RETAIN);

    if(artwork == nil) {
        self.image = artwork.placeholder;
        [self setNeedsLayout];
    } else {
        
        
        if(artwork.thumbnail != nil) {
            self.image = artwork.thumbnail;
            [self setNeedsLayout];
        } else if(artwork.image != nil) {
            self.image = artwork.image;
            [self setNeedsLayout];
            
        }
//        else if(artwork.URLString != nil) {
        
//            [self sd_setImageWithURL:[NSURL URLWithString:artwork.URLString] placeholderImage:artwork.placeholder options:SDWebImageRetryFailed];
//            self.image = [UIImage imageWithData:[self getImageDataWithUrl:artwork.URLString]];
        
//            UIImage *cacheImge = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:artwork.URLString];
//            
//            if (cacheImge) {
//                self.image = cacheImge;
//            }else{
//              [self sd_setImageWithURL:[NSURL URLWithString:artwork.URLString] placeholderImage:artwork.placeholder options:SDWebImageRetryFailed];
//            }
//            
//
//        }
        else {
                UIImage *cacheImge = [[SDImageCache sharedImageCache] imageFromMemoryCacheForKey:artwork.URLString];
            if ([[PPImageTransaction sharedInstance] imageForKey:artwork.transactionIdentifier]) {
                self.image = [[PPImageTransaction sharedInstance] imageForKey:artwork.transactionIdentifier];
            }else{
                if (cacheImge) {
                    self.image = cacheImge;
                }else{
                     [self sd_setImageWithURL:[NSURL URLWithString:artwork.URLString] placeholderImage:artwork.placeholder options:SDWebImageRetryFailed];
//                    SDWebImageDownloader *downLoader = [SDWebImageDownloader sharedDownloader];
//                    downLoader.shouldDecompressImages = NO;
//                    [[SDWebImageDownloader sharedDownloader] downloadImageWithURL:[NSURL URLWithString:artwork.URLString] options:SDWebImageDownloaderIgnoreCachedResponse progress:^(NSInteger receivedSize, NSInteger expectedSize) {
//                        
//                    } completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
//                        self.image = cacheImge;
//                    }];
                }
            }
            
            
        }
    }
}
- (PPAbstractArtwork *)artwork {
    return objc_getAssociatedObject(self, "artwork");
}

- (UIControl *)control {
    UIControl *control = objc_getAssociatedObject(self, "control");

    if(control == nil) {
        control = [[UIControl alloc] init];
        [self addSubview:control];

        [control mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self);
        }];

        objc_setAssociatedObject(self, "control", control, OBJC_ASSOCIATION_RETAIN);
    }

    return control;
}

- (void)setActionBlock:(UIControlBlock)actionBlock forEvent:(UIControlEvents)event {

    self.userInteractionEnabled = YES;

    [self.control setActionBlock:actionBlock forControlEvents:event];
}

- (void)setCoverImageWithAlbum:(PPAlbumModel *)album {

    NSArray <PPAlbumImageModel *> *images = [PPAlbumImageModel where:@{@"auuid": album.auuid, @"deleted": @(NO)}];

    __block PPAlbumImageModel *coverImage = nil;
    [images enumerateObjectsUsingBlock:^(PPAlbumImageModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        if(obj.isCover.boolValue) {
            coverImage = obj;
            *stop = YES;
        }
    }];

    if(coverImage != nil) {
        self.artwork = coverImage.artwork;
    } else if(images.count == 0) {
        self.image = [UIImage imageNamed:@"album-nothing"];
        [self setNeedsLayout];
    } else if(album.coverURLString.length > 0) {
        [self sd_setImageWithURL:[NSURL URLWithString:album.coverURLString] placeholderImage:nil options:SDWebImageRetryFailed];
    } else if(images.count > 0) {
        self.artwork = images.firstObject.artwork;
    } else {
        self.image = [UIImage imageNamed:@"album-nothing"];
        [self setNeedsLayout];
    }
}

@end
